﻿using System.Data.SQLite;

namespace RobosimAPI.Class
{
	public class Event
	{
        public static int evevt_count=0;
		/*
		 * CREATE TABLE event (
        event_id     INTEGER PRIMARY KEY AUTOINCREMENT,
        event_status INTEGER,
        event_test   INTEGER,
        event_title  TEXT,
        event_url    TEXT,
        start_time   INTEGER,
        top          INTEGER,
        db_name      TEXT
        );
		*/
		public int event_id;
        public int event_status;
        public int event_test;
        public string? event_title;
        public string? event_url;
        public long start_time;
        public int top;
        public string? db_name;
        public static List<Event> GetEvents(int start, int count)
        {
            List<Event> events = new List<Event>(); 
            string sql = "SELECT * FROM event LIMIT @start, @count";
            using (SQLiteConnection conn = new SQLiteConnection(Account.DB_CONNECTION_STRING))
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@start", start);
                    cmd.Parameters.AddWithValue("@count", count);
                    using (SQLiteDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Event e = new Event();
                            e.event_id = reader.GetInt32(0);
                            e.event_status = reader.GetInt32(1);
                            e.event_test = reader.GetInt32(2);
                            e.event_title = reader.GetString(3);
                            e.event_url = reader.GetString(4);
                            e.start_time = reader.GetInt64(5);
                            e.top = reader.GetInt32(6);
                            e.db_name = reader.GetString(7);
                            events.Add(e);
                        }
                    }
                }
                conn.Close();
            }
            return events;
        }
        public Group[] GetGroups()
        {
            List<Group> groups = new List<Group>();
            string sql = "SELECT * FROM [group] WHERE event_id = @event_id";
            using (SQLiteConnection conn = new SQLiteConnection(Account.DB_CONNECTION_STRING))
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@event_id", event_id);
                    using (SQLiteDataReader reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Group g = new Group();
                            g.id = reader.GetInt32(0);
                            g.event_id = reader.GetInt32(1);
                            g.title = reader.GetString(2);
                            g.can_build = reader.GetInt32(3);
                            g.can_remote = reader.GetInt32(4);
                            g.cover = reader.GetString(5);
                            g.face_timing = reader.GetInt32(6);
                            g.need_face_auth = reader.GetInt32(7);
                            g.need_face_compare = reader.GetInt32(8);
                            groups.Add(g);
                        }
                    }
                }
                conn.Close();
            }
            return groups.ToArray();
        }
        public static void GetEventCount()
        {
            string sql = "SELECT COUNT(*) FROM event";
            using (SQLiteConnection conn = new SQLiteConnection(Account.DB_CONNECTION_STRING))
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(sql, conn))
                {
                    evevt_count = (int)cmd.ExecuteScalar();
                }
                conn.Close();
            }
        }
        public Event(int event_id)
        {
			using (var conn = new SQLiteConnection(Account.DB_CONNECTION_STRING))
            {
                conn.Open();
                string sql = "SELECT * FROM [event] WHERE event_id = @event_id";
                using (var cmd = new SQLiteCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@event_id", event_id);
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            this.event_id = reader.GetInt32(0);
                            this.event_status = reader.GetInt32(1);
                            this.event_test = reader.GetInt32(2);
                            this.event_title = reader.GetString(3);
                            this.event_url = reader.GetString(4);
                            this.start_time = reader.GetInt64(5);
                            this.top = reader.GetInt32(6);
                            this.db_name = reader.GetString(7);
                        }
                    }
                }
                conn.Close();
                conn.Dispose();
            }

		}
        public Event() { }
	}
}
